package com.mdd.common.mapper.order;

import com.mdd.common.core.basics.IBaseMapper;
import com.mdd.common.entity.order.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 订单Mapper
 * @author panweiliang
 */
@Mapper
public interface OrderMapper extends IBaseMapper<Order> {


    @Select({"SELECT COUNT(id) AS orderCount FROM ls_order WHERE order_status = #{orderStatus} AND delete_time IS NULL "})
    Long countByOrderStatus(Integer orderStatus);

    @Select({"SELECT COUNT(id) AS orderCount FROM ls_order WHERE delete_time IS NULL "})
    Long countAll();


    @Select({"SELECT SUM(total_amount) FROM ls_order WHERE pay_status = 1 AND delete_time IS NULL "})
    BigDecimal getSalesAmount();

    @Select({"SELECT IFNULL(SUM(total_amount), 0) FROM ls_order WHERE pay_status = 1 AND pay_time >= #{start} AND pay_time <= #{end} AND delete_time IS NULL "})
    BigDecimal getSalesAmountByTimestamp(@Param("start") Long start, @Param("end") Long end);

    @Select({"SELECT d.date, IFNULL(r.num,0) AS num FROM (SELECT CURDATE() AS date UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 1 DAY) AS date UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 2 DAY) AS date UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 3 DAY) AS date UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 4 DAY) AS date UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 5 DAY) AS date UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 6 DAY) AS date UNION ALL SELECT DATE_SUB(CURDATE(), INTERVAL 7 DAY) AS date ) d LEFT JOIN ( SELECT FROM_UNIXTIME(pay_time,'%Y-%m-%d') AS pay_time, sum(total_amount) AS num FROM ls_order WHERE pay_status = 1 GROUP BY FROM_UNIXTIME(pay_time,'%Y-%m-%d')) r ON r.pay_time = d.date GROUP BY d.date"})
    List<Map<String, BigDecimal>> getSalesAmountFromPast7days();

    @Select({"SELECT COUNT(id) FROM ls_order WHERE pay_status = 1 AND delete_time IS NULL "})
    Long getSalesOrderCount();


    @Select({"SELECT * FROM ls_order WHERE order_status = 1 AND unix_timestamp() - create_time > #{overTime}"})
    List<Order> getOvertimeOrders(Long overTime);

    @Select({"SELECT COUNT(id) FROM ls_order WHERE pay_status = 1 AND pay_time >= #{start} AND pay_time <= #{end} AND delete_time IS NULL "})
    Long getSalesOrderCountByTimestamp(@Param("start") Long start, @Param("end") Long end);
}
